let api = module.exports;
let pomelo = require('pomelo');
let logger = require('pomelo-logger').getLogger('pomelo');
let token = require('../util/token');

api.roomMessagePush = function (msg, uidAndFrontendIdArr) {
    return new Promise((resolve, reject) => {
        let channelService = pomelo.app.get('channelService');
        let curMsg = JSON.parse(JSON.stringify(msg));
        curMsg.pushRouter = 'RoomMessagePush';
        curMsg.data = token.createMsg(curMsg.data);
        channelService.pushMessageByUids('ServerMessagePush', curMsg, uidAndFrontendIdArr, {}, function (err) {
            if (!!err) {
                logger.error(err.stack);
                reject(err);
            } else {
                resolve();
            }
        });
    });
};

api.gameMessagePush = function (msg, uidAndFrontendIdArr) {
    return new Promise((resolve, reject) => {
        let channelService = pomelo.app.get('channelService');
        let curMsg = JSON.parse(JSON.stringify(msg));
        curMsg.pushRouter = 'GameMessagePush';
        curMsg.data = token.createMsg(curMsg.data);
        channelService.pushMessageByUids('ServerMessagePush', curMsg, uidAndFrontendIdArr, {}, function (err) {
            if (!!err) {
                logger.error(err.stack);
                reject(err);
            } else {
                resolve();
            }
        });
    });
};

api.selfEntryRoomPush = function (msg, uidAndFrontendIdArr) {
    return new Promise((resolve, reject) => {
        let channelService = pomelo.app.get('channelService');
        msg.pushRouter = 'SelfEntryRoomPush';
        channelService.pushMessageByUids('ServerMessagePush', msg, uidAndFrontendIdArr, {}, function (err) {
            if (!!err) {
                logger.error(err.stack);
                reject(err);
            } else {
                resolve();
            }
        });
    });
};

api.updateUserInfoPush = function (msg, uidAndFrontendIdArr) {
    return new Promise((resolve, reject) => {
        let channelService = pomelo.app.get('channelService');
        msg.pushRouter = 'UpdateUserInfoPush';
        channelService.pushMessageByUids('ServerMessagePush', msg, uidAndFrontendIdArr, {}, function (err) {
            if (!!err) {
                logger.error(err.stack);
                reject(err);
            } else {
                resolve(true);
            }
        });
    });
};


api.kickUserPush = function (msg, uidAndFrontendIdArr) {
    return new Promise((resolve, reject) => {
        let channelService = pomelo.app.get('channelService');
        msg.pushRouter = 'KickUserPush';
        channelService.pushMessageByUids('ServerMessagePush', msg, uidAndFrontendIdArr, {}, function (err) {
            if (!!err) {
                logger.error(err.stack);
                reject(err);
            } else {
                resolve(true);
            }
        });
    });
};

api.gameInvitation = function (msg, uidAndFrontendIdArr) {
    return new Promise((resolve, reject) => {
        let channelService = pomelo.app.get('channelService');
        msg.pushRouter = 'gameInvitation';
        channelService.pushMessageByUids('ServerMessagePush', msg, uidAndFrontendIdArr, {}, function (err) {
            if (!!err) {
                logger.error(err.stack);
                reject(err);
            } else {
                resolve(true);
            }
        });
    });
};

api.joinGameAgainPush = function (msg, uidAndFrontendIdArr) {
    return new Promise((resolve, reject) => {
        let channelService = pomelo.app.get('channelService');
        msg.pushRouter = 'JoinGameAgainPush';
        channelService.pushMessageByUids('ServerMessagePush', msg, uidAndFrontendIdArr, {}, function (err) {
            if (!!err) {
                logger.error(err.stack);
                reject(err);
            } else {
                resolve(true);
            }
        });
    });
};


api.broadcastPush = function (msg) {
    return new Promise((resolve, reject) => {
        let channelService = pomelo.app.get('channelService');
        msg.pushRouter = 'BroadcastPush';
        channelService.broadcast('connector', 'ServerMessagePush', msg, null, function (err) {
            if (!!err) {
                logger.error(err.stack);
                reject(err);
            } else {
                resolve();
            }
        });
    });
};

api.popDialogContentPush = function (msg, uidAndFrontendIdArr) {
    return new Promise((resolve, reject) => {
        let channelService = pomelo.app.get('channelService');
        msg.pushRouter = 'PopDialogContentPush';
        channelService.pushMessageByUids('ServerMessagePush', msg, uidAndFrontendIdArr, {}, function (err) {
            if (!!err) {
                logger.error(err.stack);
                reject(err);
            } else {
                resolve();
            }
        });
    });
};

api.gameAwardPush = function (msg, uidAndFrontendIdArr) {
    return new Promise((resolve, reject) => {
        let channelService = pomelo.app.get('channelService');
        msg.pushRouter = 'gameAwardPush';
        channelService.pushMessageByUids('ServerMessagePush', msg, uidAndFrontendIdArr, {}, function (err) {
            if (!!err) {
                logger.error(err.stack);
                reject(err);
            } else {
                resolve(true);
            }
        });
    });
};
